<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传头像测试</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<form method="POST" enctype="multipart/form-data" id="changePhoto">
    头像上传:<input type="file" name="myimg" id="file"/>
    <input type="submit" value="上传" id="up">
</form>
<script src="js/common.js"></script>
<script>
    $(document).ready(function () {
        $('#changePhoto').on('submit', function (e) {
            e.preventDefault(); // 阻止表单默认提交行为
            var formData = new FormData(this);

            $.ajax({
                url: '/user/updateUserInfo',
                type: 'post',
                data: formData,
                contentType: false, // 由于向服务器发送的是FormData对象，必须设置为false
                processData: false, // 禁止JQuery转换数据，因为我们需要的是FormData对象
                success: function (data) {
                    console.log(data);
                    // $('#avatar').attr('src', data); // 更新头像图片的src
                    alert("头像更新成功");
                    location.href = "blog_list.html";
                },
                error: function (result) {
                    console.log(result);
                    alert("头像更新失败");
                }
            });
        });
    });
</script>

</body>
</html>